package models;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.trackvoodoo.models.UserTrackDaysDocument.UserTrackDays;
import com.trackvoodoo.models.UserTrackDaysDocument.UserTrackDays.UserTrackDayElement;

import database.connector.DatabaseFactory;
import database.live.DOAStatements;
import database.live.MySqlDAO;
import engine.EngineUtils;

public class UserTrackDaysModel {

	private MySqlDAO d = null;
	private UserTrackDays uTrack = null;
	
	public UserTrackDaysModel()
	{
		d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			try{
				uTrack = UserTrackDays.Factory.newInstance();
				ResultSet rs = d.selectQuery(DOAStatements.SELECT_USERTRACKDATES); //idusertrackdays, trackRef, date, userRef, compelete, carRef
				while (rs.next())
				{
					UserTrackDayElement e = uTrack.addNewUserTrackDayElement();
					e.setIdUserTrackDay(rs.getInt(1));
					e.setTrackRef(rs.getInt(2));
					e.setDate(EngineUtils.dateToCalendar(rs.getDate(3)));
					e.setUserRef(rs.getInt(4));
					e.setComplete(rs.getBoolean(5));
					e.setCarRef(rs.getInt(6));
				}
			
			}catch (SQLException e)
			{
				e.printStackTrace();
			}finally
			{
				d.closeConnection();
			}
		}
	}

	public UserTrackDays getUserTrackDays()
	{
		return uTrack;
	}
}
